package com.xxy.controller;

import com.xxy.context.BaseContext;
import com.xxy.entity.Message;
import com.xxy.entity.dto.MessageQueryDTO;
import com.xxy.entity.vo.MessageVO;
import com.xxy.result.PageResult;
import com.xxy.result.Result;
import com.xxy.service.IMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/message")
public class MessageController {
    @Autowired
    private IMessageService messageService;

    @PostMapping("/save")
    public Result<String> save(Message message) {
       messageService.save(message);
       return Result.success("保存成功");
    }
    @DeleteMapping("/delete/{id}")
    public Result<String> delete(@PathVariable Long id) {
        messageService.removeById(id);
        return Result.success("删除成功");
    }

    @PostMapping("/queryByFilter")
    public Result<List<MessageVO>> queryByFilter(@RequestBody MessageQueryDTO messageQueryDTO) {
        List<MessageVO> list = messageService.queryByFilter(messageQueryDTO);
        return Result.success(list);
    }
    @PostMapping("/query")
    public Result<PageResult> query(@RequestBody MessageQueryDTO messageQueryDTO) {
        PageResult pageResult = messageService.query(messageQueryDTO);
        return Result.success(pageResult);
    }

    //将自己的所有消息设为已读
    @PutMapping("/setIsRead")
    public Result<String> setIsRead() {
        Long userId = BaseContext.getCurrentId();
        messageService.lambdaUpdate()
                .eq(Message::getUserId, userId)
                .set(Message::getIsRead, 1)
                .update();
        return Result.success("设置成功");
    }

}
